package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.MarketAdService;
import com.cskaoyan.market.service.MarketAdServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/ad/*")
@MultipartConfig
public class AdminAdvertisingController extends HttpServlet {
    private MarketAdService adService = new MarketAdServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/ad/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }


    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/ad/", "");
        if("create".equals(op)) {
            create(req, resp);
        }
        if("update".equals(op)) {
            update(req, resp);
        }
        if ("delete".equals(op)) {
            delete(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAd marketAd = objectMapper.readValue(responseBody, MarketAd.class);
        adService.update(marketAd);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }


    /*
    *     private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取位于请求体里面的json字符串
        String requestBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        AddGoodsBo addGoodsBo = objectMapper.readValue(requestBody, AddGoodsBo.class);
        //商品上架的逻辑：将这些数据存储到几张表中
        goodsService.create(addGoodsBo.getGoods(), addGoodsBo.getSpecifications(), addGoodsBo.getProducts(), addGoodsBo.getAttributes());

        //ok
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
}*/
    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAd marketAd = objectMapper.readValue(responseBody, MarketAd.class);
        adService.create(marketAd);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

    void list(HttpServletRequest req, HttpServletResponse resp){
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        int page = 0;
        int limit = 0;
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        }catch (NumberFormatException e) {
            ResponseUtil.fail(404,"page和limit不能为非正整数");
        }
        List<MarketAd> marketAdList = adService.list(page, limit, sort, order, name, content);
        Object o = ResponseUtil.okList(marketAdList);
        try {
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String responseBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAd marketAd = objectMapper.readValue(responseBody, MarketAd.class);
        adService.delete(marketAd.getId());
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }
}
